# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
import re

import requests
from scrapy import Request
from scrapy.pipelines.images import ImagesPipeline
from ZolSpider.fileUtil import makedirs, findfile

from ZolSpider.fileUtil import makedirs
from ZolSpider.settings import IMG_PATH


class ZolspiderPipeline(ImagesPipeline):

    def save_file(self, file, content):
        f = open(file, 'wb')
        f.write(content)
        f.close()

    def get_content(self, url):
        # print(url)
        html = requests.get(url=url)
        return html.content

    def process_item(self, item, spider):
        fileurl = item['imgUrl'][0]
        # filename = os.path.split(fileurl)[1]

        # filepath = IMG_PATH + '\\' + item['dirsName'] + '\\'
        # filename = filepath + item['title'] +'.jpg'

        filename = item['dirsName'] + '\\' + item['title'] +'.jpg'
        # self.makedirs(filepath)
        # makedirs(filepath)

        # 判断图片是否已下载
        if os.path.isfile(filename):
            print('------文件已存在！')
            print(filename)
            return

        if findfile(IMG_PATH, item['title'] + '.jpg'):
            print('------该文件已创建！')
            print(item['title'] + '.jpg')
            return
        # print('*'*20)
        # print(item)
        # print(fileurl)
        # print(filename)
        self.save_file(filename, self.get_content(fileurl))
